package gererg;

/**
 * @author leon(liangzou0318@gmail.con)
 * @date 2012-12-6
 * @filanane NQueen.java
 */
public class NQueen {
	public static int ite = 0;
	static boolean place(int k,int n,int a[]){
		//System.out.print(k+"   "+n);
		
//		if(k==0) return true;
		for(int i=0;i<k;i++){			
			if((a[i]==n)||(Math.abs(a[i]-n)==Math.abs(i-k))) return false;
			//else System.out.println("at");
		   }
        return true;		
	}
	static void  NQueens(int k,int n,int a[]){
		
		for(int i=0;i<n;i++){   //这里的n代表每次都可以在n列中选择一个
			if(ite==1) break;
			else if(place(k,i,a)){   //判断第k行i列是否符合规则
			//	System.out.println("  n : "+n+"  mmd k:"+k+" i: "+i);
				a[k]=i;				
				if(k==n-1){
					System.out.print("result is: ");
					for(int t=0;t<n;t++){					 
				    	System.out.print(a[t]+" ");
				    	}	
					
					System.out.println();
					ite=1;
				}
				else NQueens(k+1,n,a);
			}
		}
	}
   
	public static void main(String[] args) {
		// TODO Auto-generated nethod stub
		//public static  int re=0;
       //  NQueen nqueen=new NQueen();
         int temp=4;
         int[] a=new int[temp];
         System.out.println("Start:");
         NQueens(0, temp, a);
        
	}

}
